package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.BacksaleDetail;
import com.sword.pojo.stock.Backstock;
import com.sword.pojo.stock.BackstockDetail;
import com.sword.service.impl.stock.BackStockDetailServiceImpl;
import com.sword.service.impl.stock.BackStockServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/stock")
public class BackStockController {
    @Autowired
    private BackStockServiceImpl backStockService;

    @Autowired
    private BackStockDetailServiceImpl backStockDetailService;

    @RequestMapping("/backstock")
    public String backstock() {
        return "views/stock/backStock";
    }

    @RequestMapping("/backstockdetail")
    public String backstockdetail() {
        return "views/stock/backStockDetail";
    }

    /**
   查询所有退库单
    */
    @RequestMapping("/getBackstocks")
    @ResponseBody
    public PageResult queryBackstock(@RequestBody QueryPageBean queryPageBean) {
        try {
            return backStockService.queryBackStock(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BACKSTOCK_FAIL, null, null);
        }
    }
    /**
     * 添加退库单功能
     */

    @ResponseBody
    @RequestMapping("/addBackstock")
    public Result addBackstock(@RequestBody Backstock backstock) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            backstock.setEmployeeid(userId);
            // 判断是否添加成功
            Integer flag = backStockService.addBackStock(backstock);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BACKSTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BACKSTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BACKSTOCK_SUCCESS, null);
    }
    /**
     * 修改退库单
     */
    @ResponseBody
    @RequestMapping("editBackstock")
    public Result editBackstock(@RequestBody Backstock backstock) {
        try {
            Integer flag = backStockService.editBackStock(backstock);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSTOCK_SUCCESS, null);
    }

    /**
     * 删除退库单
     */

    @ResponseBody
    @RequestMapping("deleteBackstock")
    public Result deleteBackstock(Integer id) {
        try {
            Integer flag = backStockService.deleteBackStock(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BACKSTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BACKSTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BACKSTOCK_SUCCESS, null);
    }



    /**
     查询所有退库明细
 */
    @RequestMapping("/getBackstockdetails")
    @ResponseBody
    public PageResult queryBackstockdetails(@RequestBody QueryPageBean queryPageBean) {
        try {
            return backStockDetailService.queryBackStockDetail(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BACKSTOCKDETAIL_FAIL, null, null);
        }
    }
    /**
     * 添加退库明细功能
     */

    @ResponseBody
    @RequestMapping("/addBackstockdetail")
    public Result addBackstockdetail(@RequestBody BackstockDetail backstockDetail) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            backstockDetail.setCreateid(userId);
            backstockDetail.setStatus(0);
            // 判断是否添加成功
            Integer flag = backStockDetailService.addBackStockDetail(backstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BACKSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BACKSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BACKSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 修改退库明细
     */
    @ResponseBody
    @RequestMapping("editBackstockdetail")
    public Result editBackstockdetail(@RequestBody BackstockDetail backstockDetail) {
        try {
            Integer flag = backStockDetailService.editBackStockDetail(backstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 审核退库明细
     */
    @ResponseBody
    @RequestMapping("examineBackstockdetail")
    public Result examineBackstockdetail(@RequestBody BackstockDetail backstockDetail) {
        try {
            Integer flag = backStockDetailService.examineBackStockDetail(backstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 弃审退库明细
     */
    @ResponseBody
    @RequestMapping("unexamineBackstockdetail")
    public Result unexamineBackstockdetail(@RequestBody BackstockDetail backstockDetail) {
        try {
            Integer flag = backStockDetailService.unexamineBackStockDetail(backstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 删除退库明细
     */

    @ResponseBody
    @RequestMapping("deleteBackstockdetail")
    public Result deleteBackstockdetail(Integer id) {
        try {
            Integer flag = backStockDetailService.deleteBackStockDetail(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BACKSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BACKSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BACKSTOCKDETAIL_SUCCESS, null);
    }
}
